package com.huluobo.a2109mouthdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.huluobo.a2109mouthdemo.adapter.MyFragmentAdapter;
import com.huluobo.a2109mouthdemo.addFriend.AddFriendActivity;
import com.huluobo.a2109mouthdemo.addGroup.AddGroupActivity;
import com.huluobo.a2109mouthdemo.contact.ContactFragment;
import com.huluobo.a2109mouthdemo.conversation.ConversationFragment;
import com.huluobo.a2109mouthdemo.mine.MineFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private BottomNavigationView bottomNavigationView;
    private List<Fragment> fragments;
    private MyFragmentAdapter fragmentAdapter;

    private TextView title;
    private ImageView add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.main_vp);
        bottomNavigationView = findViewById(R.id.main_nav);
        title = findViewById(R.id.header_title);
        add = findViewById(R.id.header_add);
        add.setVisibility(View.VISIBLE);

        add.setOnClickListener(v -> {
            PopupMenu popupMenu = new PopupMenu(this, add);
            popupMenu.inflate(R.menu.add_menu);
            popupMenu.setOnMenuItemClickListener(item -> {
                switch (item.getItemId()) {
                    case R.id.addFriend:
                        Intent intent = new Intent(this, AddFriendActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.addGroup:
                        Intent intentAddGroup = new Intent(this, AddGroupActivity.class);
                        startActivity(intentAddGroup);
                        break;
                    case R.id.scan:
                        Toast.makeText(this, "扫一扫", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.pay:
                        Toast.makeText(this, "收付款", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            });
            popupMenu.show();
        });

        fragments = new ArrayList<>();
        fragments.add(new ConversationFragment());
        fragments.add(new ContactFragment());
        fragments.add(new MineFragment());

        fragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                MenuItem menuItem = bottomNavigationView.getMenu().getItem(position);
                switch (menuItem.getItemId()) {
                    case R.id.tab_conversation:
                        title.setText("微信");
                        add.setVisibility(View.VISIBLE);
                        break;
                    case R.id.tab_contact:
                        title.setText("联系人");
                        add.setVisibility(View.VISIBLE);
                        break;
                    case R.id.tab_mine:
                        title.setText("我的");
                        add.setVisibility(View.GONE);
                        break;
                }
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.tab_conversation:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.tab_contact:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.tab_mine:
                        viewPager.setCurrentItem(2);
                        break;
                }

                return true;
            }
        });
    }
}